package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.internship.busi.dto.CardStudentVO;
import com.edu.internship.busi.entity.CardList;
import com.edu.internship.busi.entity.Student;
import com.edu.internship.busi.mapper.CardStudentMapper;
import com.edu.internship.busi.service.ICardListService;
import com.edu.internship.busi.service.IStudentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Condition;


/**
 * code is far away from bug with the animal protecting
 *
 * @author longjihao
 * @description <p>查询消费信息 的controller</p>
 * @date 2020-08-13
 */
@RestController
@RequestMapping("/CardStudent")
@Slf4j
public class CardStudentController {

//    @Resource
//    ICardListService cardListService;
//    @Resource
//    IStudentService studentService;
//
//    @GetMapping("/get")
//    @ApiOperation(value = "查询",notes = "查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "current", value = "当前页数", dataTypeClass = Long.class, paramType = "query", defaultValue = "1"),
//            @ApiImplicitParam(name = "size", value = "每页几条记录", dataTypeClass = Long.class, paramType = "query", defaultValue = "10")
//    })
//    public IPage<CardList> page(String cId, String name, String cla, @RequestParam(defaultValue = "1") Long current,
//                                @RequestParam(defaultValue = "10") Long size){
//
//        LambdaQueryWrapper<Student> stQw = Wrappers.lambdaQuery();
//        stQw.like(StringUtils.isNotBlank(name),Student::getStudentName,name)
//                .like(StringUtils.isNotBlank(cla),Student::getClassId,cla);
//        List<Student> stLi = studentService.list(stQw);
//
//        LambdaQueryWrapper<CardList> caQw = Wrappers.lambdaQuery();
//        for (int i = 0; i < stLi.size(); i++) {
//            if (i == 0) {
//                caQw.like(StringUtils.isNotBlank(stLi.get(i).getStudentNumber()),CardList::getStudentId,stLi.get(i).getStudentNumber());
//            } else {
//                caQw.or().like(CardList::getStudentId,stLi.get(i).getStudentNumber());
//            }
//        }
//        caQw.like(StringUtils.isNotBlank(cId),CardList::getCardNo,cId);
//
//
//        return cardListService.page(new Page<>(current, size), caQw);
//    }

    @Autowired
    CardStudentMapper cardStudentMapper;

    @ApiOperation(value = "查询",notes = "查询")
    @GetMapping("get")
    public IPage<CardStudentVO> dynamicQuery(Page<CardStudentVO> page, String studentName,String classId, String cardId ,String costTime) {
        QueryWrapper<CardStudentVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(studentName), "t_student.student_name", studentName);
        queryWrapper.like(StringUtils.hasText(classId), "t_student.class_id", classId);
        queryWrapper.like(StringUtils.hasText(cardId), "t_card.card_no", cardId);
        queryWrapper.like(StringUtils.hasText(costTime), "t_card.cost_time", costTime).orderByDesc("t_card.cost_time");


//        queryWrapper.like(StringUtils.hasText(inTime), "t_card.in_time", inTime);
//        ,String costTime,String inTime

//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = sdf.format(date);
//        queryWrapper.lambda()
//                .ge(CardStudentVO::getCostTime,format)
//                .le(CardStudentVO::getInTime,format);

        queryWrapper.apply("t_student.student_number = t_card.student_id");
        return cardStudentMapper.findCardStudent(page, queryWrapper);
    }

}